package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询所有
    @GetMapping("/category/findAll")
    public ResultInfo findAll() {
        //调用service查询所有
        List<Category> list = categoryService.findAll();
        return ResultInfo.success(list);
    }

    //菜品添加
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {//接受post参数
        //调用service层添加方法
        categoryService.save(category);
        return ResultInfo.success();
    }

    //菜品修改
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category) {
        //调用service层方法修改
        categoryService.update(category);
        return ResultInfo.success();
    }

    //不抛异常的菜品删除
//    @DeleteMapping("/category")
//    public ResultInfo delete(long id){
//        //调用service层方法删除
//        ResultInfo resultInfo = categoryService.delete(id);
//        //返回结果
//        return resultInfo;
//    }
    //抛异常的菜品删除
    @DeleteMapping("/category")
    public ResultInfo delete(long id) {
        //调用service层方法删除
        categoryService.delete(id);
        //返回结果
        return ResultInfo.success();
    }


    //新增菜品/套餐时查询分类信息
    @GetMapping("/category/list")
    public ResultInfo list(Integer type){//接收参数
        //根据type查询分类
        List<Category> categoryList = categoryService.findByType(type);
        //返回结果
        return ResultInfo.success(categoryList);


    }

}
